package components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import nemeanshop.NemeanShop;
import nemeanshop.NemeanShopException;
import nemeanshop.Product;

/**
 *
 * @author Ragnar
 */
public class ProductCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer
{
    private JPanel      panel;
    private JLabel      text;
    private NButton     btn;
    private NButton     btn2;
    private Product     prod;
    private NemeanShop  ns;
    private NTablemod2  mod2;
    private NTable      parent;
    private Color mover = new Color(178,221,81);
    private Color mover2 = new Color(231,251,176);
    private Color oddrows = new Color(202,252,91);
    
    public ProductCell(final NemeanShop ns, final NTablemod2 mod2, NTable parent)
    {
        this.ns     = ns;
        this.mod2   = mod2;
        this.parent = parent;
        text        = new JLabel();
        text.setSize(200, 40);
        btn         = new NButton(new ImageIcon("src/GUInemeanshop/img/edit-delete-6.png") ,"Del");
        btn.setFocusable(false);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                int index = ns.getOrder().removeProduct(prod.getBarcode());
                mod2.fireTableRowsDeleted(index, index);
                mod2.updateBP();
                fireEditingCanceled();
            }            
        });
        btn2 = new NButton(new ImageIcon("src/GUInemeanshop/img/list-remove-5.png"), "-");
        btn2.setFocusable(false);
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                int index = ns.getOrder().hasProduct(prod.getBarcode());
                try
                {
                    if (ns.getOrder().getBasket().get(index).getTotal() > 1)
                    {
                        ns.getOrder().getBasket().get(index).decreaseQuantity();
                    }
                    else if(ns.getOrder().getBasket().get(index).getTotal() == 1)
                    {
                        btn.doClick();
                    }
                }
                catch (NemeanShopException ex)
                {
                    Logger.getLogger(ProductCell.class.getName()).log(Level.SEVERE, null, ex);
                }
                mod2.fireTableRowsUpdated(index, index);
                mod2.updateBP();
                fireEditingCanceled();
            }
        });
        
        NPanel p = new NPanel(new GridLayout(1,3));
        p.add(btn2);
        p.add(btn);
        panel = new JPanel(new BorderLayout());
        panel.add(text, BorderLayout.WEST);
        panel.add(p, BorderLayout.EAST);
        //panel.add(btn2, BorderLayout.CENTER);
    }
    
    private void updateData(Product prod, boolean isSelected, JTable table, int row, int col)
    {
        this.prod = prod;
        this.text.setText("<html><table width='350'><tr><td width='150'><b>"+this.prod.getProductName()+"</b></td><td width='100'>"+this.prod.getTotal()+"</td><td width='100'>"+(this.prod.getSellPrice())+"</td></tr></table></html>");
       
        
        
        if(parent.getRovv() == row)
       {
          panel.setBackground(this.oddrows);
          btn.setBackground(Color.white);
          panel.setForeground(Color.blue);
       }
       else
       {
           if (row % 2 == 0 && !table.isCellSelected(row, col)) {
            panel.setBackground(Color.white);
            } 
            else {
            panel.setBackground(this.mover2);
          }
       }
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        Product prod = (Product)value;
        updateData(prod, false, table, row, column);
        return panel;
    }
    
    public Object getCellEditorValue()
    {
        return null;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Product prod = (Product)value;
        updateData(prod, isSelected, table, row, column);
        return panel;
    }
    
    
}

